package com.intellij.licensecommon.f.impl;


import com.intellij.licensecommon.f.Protocol;
import com.intellij.licensecommon.f.b;
import com.intellij.licensecommon.f.c;
import com.intellij.licensecommon.message.Message;
import com.intellij.licensecommon.util.MessageCache;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.io.ByteArrayInputStream;
import java.io.IOException;


public class a implements Protocol {
    private final f a;
    private final MessageCache cache = new MessageCache();
    private static final long c = a(15L);
    private static final SAXBuilder d = new SAXBuilder();

    public a() {
        this.a = new f();
    }

    public byte[] a(Message paramMessage)
            throws c {
        return a(this.a.a(paramMessage));
    }

    public Message a(byte[] paramArrayOfByte)
            throws b {
        String str = new String(paramArrayOfByte);

        Message localMessage1 = this.cache.b(str);
        if (this.cache.a(str)) {
            return localMessage1;
        }
        Element localElement = b(paramArrayOfByte);
        Message localMessage2 = e.a(localElement);
        this.cache.a(str, localMessage2);
        return localMessage2;
    }

    private byte[] a(Element paramElement) {
        this.cache.a(c);
        if (paramElement == null) {
            return new byte[0];
        }
        return new XMLOutputter().outputString(paramElement).getBytes();
    }

    private static long a(long paramLong) {
        return paramLong * 60L * 1000L;
    }

    private static Element b(byte[] paramArrayOfByte)
            throws b {
        ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
        try {
            return d.build(localByteArrayInputStream).getRootElement();
        } catch (JDOMException localJDOMException) {
            throw new b("Invalid XML", localJDOMException);
        } catch (IllegalStateException localIllegalStateException) {
            throw new b("Invalid XML", localIllegalStateException);
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
        return null;
    }
}

